package controller;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.Reader;
import java.util.ArrayList;
import java.util.List;

import org.zkoss.util.media.Media;
import org.zkoss.zhtml.Fileupload;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.Executions;
import org.zkoss.zk.ui.Path;
import org.zkoss.zk.ui.event.Event;
import org.zkoss.zk.ui.event.EventListener;
import org.zkoss.zk.ui.event.Events;
import org.zkoss.zk.ui.event.UploadEvent;
import org.zkoss.zk.ui.select.SelectorComposer;
import org.zkoss.zk.ui.select.annotation.Listen;
import org.zkoss.zk.ui.select.annotation.Wire;
import org.zkoss.zk.ui.util.Composer;
import org.zkoss.zul.A;
import org.zkoss.zul.Hlayout;
import org.zkoss.zul.Label;
import org.zkoss.zul.Textbox;
import org.zkoss.zul.Vlayout;
import org.zkoss.zul.Window;

public class MailController extends SelectorComposer<Window> {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	@Wire
	private Label label;

	@Wire
	private Label error = new Label();

	@Wire
	private List<Object> ls;

	public List<Object> getLs() {
		return ls;
	}

	public void setLs(List<Object> ls) {
		this.ls = ls;
	}

	public MailController() {
		ls = new ArrayList<Object>();
	}

	public String checkString(String str, String deleteStr) {
		String[] s = str.split("/");
		List<String> ls = new ArrayList<String>();
		for (String string : s) {
			if (!string.equals(deleteStr)) {
				ls.add(string);
			}
		}

		String strings = "";
		for (String string : ls) {
			strings += string + "/";
		}
		return strings;
	}

	public boolean checkTextBox(Textbox textbox) {
		boolean rs = false;
		if (textbox.getValue().contains("<")
				|| textbox.getValue().contains(">")
				|| textbox.getValue() == null) {
			return rs;
		}
		rs = true;
		return rs;
	}

	@Listen("onClick=#txtLocation")
	public void checkLocation() {
		Textbox textbox = (Textbox) Path.getComponent("/win/txtLocation");
		textbox.setValue("");
		error.detach();

	}

	@Listen("onClick=#txtDestination")
	public void checkDestination() {
		Textbox textbox = (Textbox) Path.getComponent("/win/txtDestination");
		textbox.setValue("");
	}

	@Listen("onUpload=#upLocation")
	public void getFileName(UploadEvent event) throws IOException {
		final Textbox textbox = (Textbox) Path.getComponent("/win/txtLocation");
		boolean rs = checkTextBox(textbox);
		if (rs) {
			final Media media = event.getMedia();
			label = new Label(media.getName());
			ls.add(label);
			final String txt = textbox.getValue() + "\"" + label.getValue() + "\"";
			final Hlayout hl = new Hlayout();
			textbox.setValue(txt);
			A rm = new A("Remove");
			rm.addEventListener(Events.ON_CLICK, new EventListener<Event>() {

				@Override
				public void onEvent(Event arg0) throws Exception {
					// TODO Auto-generated method stub
					hl.detach();
					textbox.setValue(checkString(textbox.getValue(),
							media.getName()));
				}
			});
			hl.appendChild(label);
			hl.appendChild(rm);
			Vlayout vlayout = (Vlayout) Path
					.getComponent("/win/listFileUpLoad");
			vlayout.appendChild(hl);
		} else {
			error.setValue("Path is not valid, please try again!");
			error.setStyle("color:red;");
			Vlayout vlayout = (Vlayout) Path
					.getComponent("/win/listFileUpLoad");
			vlayout.appendChild(error);
		}
	}

	@Listen("onClick=#btnSubmit")
	public void submit() throws IOException {
		String object = Executions.getCurrent().getDesktop().getWebApp()
				.getRealPath("/te");
		Textbox textbox = (Textbox) Path.getComponent("/win/txtLocation");
		Textbox textbox2 = (Textbox) Path.getComponent("/win/txtDestination");
		for (Object l : this.ls) {
			Label li = (Label) l;
			File fileName = new File(li.getValue());
			FileInputStream fin = new FileInputStream(checkString(
					textbox.getValue(), li.getValue()));
			FileOutputStream fout = new FileOutputStream(textbox2.getValue()
					+ fileName.getName());
			int data;
			while ((data = fin.read()) != -1)
				fout.write(data);
			fin.close();
			fout.close();
		}
		Vlayout vlayout = (Vlayout) Path.getComponent("/win/listFileUpLoad");
		textbox.setValue("");
		textbox2.setValue("");
		vlayout.detach();
	}

}
